#!/usr/bin/env python3.6
# -*- coding: utf-8 -*-
# @Time    : 2022/08/25 14:04
# @Author  : elegantfrog
# @FileName: demo.py.py
import cv2
import requests
import json
import os
import time
from watermarker.marker import add_mark

img_path = './test_img.jpg'


def getImg():
    cap = cv2.VideoCapture(0)
    # 捕获并处理一帧画面
    success, frame = cap.read()
    if not success:
        print('error!')
    # 关闭摄像头
    cap.release()
    # 关闭窗口
    cv2.destroyAllWindows()
    # 保存图片
    cv2.imwrite(img_path, frame)
    print('图片已保存', img_path)


def test(content):
    path = r'D:\\Project\\德清\\粮仓\\seed2022\\datas\\files\\test.json'
    if os.path.exists(path):
        with open(path, 'w+') as f:
            f.write(content)
    else:
        with open(r'D:\\Project\\德清\\粮仓\\seed2022\\datas\\files\\test.json', 'x') as f:
            f.write(content)


def testRead():
    configUrl = "https://ai.seedlight.cn:31000/config/test.json"
    response = requests.get(configUrl)
    contentJson = response.text
    durations = list(json.loads(contentJson))
    print(type(durations))


def addPrint():
    try:
        path = r'D:\\Project\\德清\\粮仓\\seed2022\\utils\\fd6a5c0f47623874_1663754627.jpg'
        filename = os.path.basename(path)
        snCode = filename.split('_')[0]
        createTime = filename.split('_')[1].split('.')[0]
        timeArray = time.localtime(int(createTime))
        timeStr = time.strftime('%Y-%m-%d %H:%M:%S', timeArray)
        text = snCode + '\n' + timeStr
        add_mark(path, out=r'D:\\Project\\德清\\粮仓\\seed2022\\utils\\test', mark=text, opacity=1, angle=0, space=80)
    except Exception as e:
        print(e)


if __name__ == '__main__':
    # content = [
    #     {
    #         "start": 37800,
    #         "len": 7200
    #     },
    #     {
    #         "start": 43200,
    #         "len": 14400
    #     },
    #     {
    #         "start": 64800,
    #         "len": 14400
    #     }
    # ]
    # print(json.dumps(content))
    # test(json.dumps(content))
    addPrint()
